const login = require('express').Router()
const body = require('body-parser')
const { User } = require('../../mongoDB/mongoDB')
login.use(body.urlencoded({extended: false}))
login.post('*',async (req,res)=> {
	const rb = JSON.parse(Object.keys(req.body))
	let isUser = ''
	let method = ''
	if(rb.method == 0) {
		method = true
		isUser = await User.findOne({username: rb.username, password: rb.password})
	}else if(rb.method == 1) {
		method = false
		isUser = (rb.username == 'songyuening' && rb.password == 'songyuening') ? true : false
	}
	
	const option = {
		token: false,
		status: 0,
		message: '',
		method: method
	}
	if(isUser) {
		option.token = true
		option.status = 200
		option.message = '登陆成功'
	}else {
		option.token = false
		option.status = 400
		option.message = '账号或密码错误，登陆失败'
	}
	res.send(option)
})

module.exports = login